<template>
	<view class="padding20">
		<view class="card padding30">
			<text class="txt_name">医院：{{ model.name }}</text>
			<text>身份证号：{{ model.idcard }}</text>
			<text>体检人：{{ model.realname }}</text>
		</view>
		<view class="imgs">
			<view class="img">
				<uni-file-picker v-model:value="defaultImages" :disabled="isUpload"
					:image-styles="{ width: 100, height: 100 }" @select="upLoadPic" @delete="deleteImage"
					:del-icon="!isUpload" limit="6" title="报告图片"></uni-file-picker>
			</view>
		</view>
		<button v-if="!isUpload" class="btn" @click="submit">提交</button>
	</view>
</template>

<script>
	import {
		HTTP_IMG_STATIC
	} from "@/config/app.js";
	import {
		upReport
	} from "@/api/trading.js";
	export default {
		data() {
			return {
				model: {},
				images: [],
				defaultImages: [],
				HTTP_IMG_STATIC,
				isUpload: false,
			};
		},
		onLoad(options) {
			this.isUpload = false;
			this.model = {
				...JSON.parse(options.desc || JSON.stringify({}))
			};
			const _a = (this.model.image || '').split(",") || [];
			let _arr = [];
			_a.forEach((item) => {
				if (item) {
					_arr.push({
						extname: item.split(".")[1],
						url: HTTP_IMG_STATIC + "/" + item,
						name: item,
					});
				}
			});
			this.defaultImages = _arr;
			console.log(this.defaultImages);
			if (this.defaultImages.length) {
				this.isUpload = true;
			}
		},
		methods: {
			upLoadPic(e) {
				let that = this;
				that.$util.uploadImage({
							url: "sys/common/upload",
							name: "file",
							tempFilePaths: e.tempFilePaths,
						},
						function(res) {
							const {
								message
							} = res;
							if (message) {
								that.images.push(message);
							}
						}
					),
					function(err) {
						that.$util.Tips({
							title: err,
						});
					};
			},
			deleteImage(e) {
				const _name = e.tempFile.name.split(".")[0] || "";
				this.images = this.images.filter((item) => item.split("_")[0] != _name);
			},
			async submit() {
				let that = this;
				that.model.image = this.images.join();
				if (!that.model.image)
					return that.$util.Tips({
						title: "请上传报告图片",
					});
				upReport(that.model)
					.then((res) => {
						uni.showToast({
							title: "报告上传成功",
							icon: "success",
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					})
					.catch((res) => {
						that.$util.Tips({
							title: res,
						});
					});
			},
		},
	};
</script>
<style lang="scss" scoped>
	@import url("../../static/css/gstyle.scss");

	.card {
		display: flex;
		flex-direction: column;
		color: #818181;
		font-size: 22rpx;
		background-color: white;
		border-radius: 20rpx;

		.txt_name {
			color: black;
			font-size: 30rpx;
		}

		text {
			margin: 15rpx 0;
		}
	}

	.imgs {
		margin-top: 50rpx;

		.img_title {
			font-size: 30rpx;
			margin-bottom: 30rpx;
		}

		.img {
			display: flex;
			flex-wrap: wrap;

			image {
				width: 140rpx;
				height: 140rpx;
				margin: 0 20rpx 20rpx 0;
			}

			.up {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 140rpx;
				height: 140rpx;
				background-color: #cdcdcd;
			}
		}
	}
</style>